<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<el-form-item label="边坡名字" prop="slopeId">
        <el-select v-model="queryParams.slopeId" placeholder="请选择边坡编号" clearable style="width: 200px">
          <el-option
              v-for="dict in slopeCodeList"
              :key="dict.label"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备SN" prop="deviceSn">
        <el-input
            v-model="queryParams.deviceSn"
            placeholder="请输入设备SN"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间范围" prop="deviceSn">
        <el-date-picker
            v-model="queryParams.startTime"
            type="datetime"
            placeholder="开始时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            clearable
            @keyup.enter="handleQuery"
        />
        <el-date-picker
            v-model="queryParams.endTime"
            type="datetime"
            placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="weatherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<el-table-column label="边坡名称" fixed="left" align="center" prop="slopeId" width="280px">
        <template #default="scope">
          <dict-tag :options="slopeCodeList" :value="scope.row.slopeId"/>
        </template>
      </el-table-column>
      <el-table-column label="设备SN" align="center" prop="deviceSn" />
      <el-table-column label="数据时间" align="center" prop="createTime" />
      <el-table-column label="累计降雨量(mm)" align="center" prop="cumulativeRainfall" />
      <el-table-column label="日降雨量(mm)" align="center" prop="dailyRainfall" />
      <el-table-column label="小时降雨量(mm)" align="center" prop="hourlyRainfall" />
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="Weather">
import { listWeather, getWeather, delWeather, addWeather, updateWeather } from "@/api/slope/data/weather";
import {listSlopeCode} from "@/api/slope/info/info.js";

const { proxy } = getCurrentInstance();

const weatherList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const slopeCodeList = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    slopId:"",
    pageNum: 1,
    pageSize: 10,
    deviceSn: null,
  },
  rules: {
    deviceSn: [
      { required: true, message: "设备SN不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询气象监测数据列表 */
function getList() {
  loading.value = true;
listSlopeCode().then(response => {
    // 假设 response 是一个普通的对象
    slopeCodeList.value = Object.entries(response).map(([key, value]) => ({
      label: key,
      value: value
    }))
  })

  listWeather(queryParams.value).then(response => {
    weatherList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deviceSn: null,
    cumulativeRainfall: null,
    dailyRainfall: null,
    hourlyRainfall: null,
    minRainfall: null,
    temperature: null,
    pressure: null,
    windSpeed: null,
    windDirection: null,
    humidity: null,
    solarRadiation: null,
    deptId: null,
    createTime: null
  };
  proxy.resetForm("weatherRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.startTime = null;
  queryParams.value.endTime = null;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加气象监测数据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getWeather(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改气象监测数据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["weatherRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWeather(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWeather(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除气象监测数据编号为"' + _ids + '"的数据项？').then(function() {
    return delWeather(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('data/weather/export/', {
    ...queryParams.value
  }, `weather_${new Date().getTime()}.xlsx`)
}

getList();
</script>
